"""Definition of the Plan content type
"""

from AccessControl import ClassSecurityInfo

from zope.interface import implements, directlyProvides

from Products.Archetypes import atapi
from Products.ATContentTypes.content import base
from Products.ATContentTypes.content import schemata
from Products.Archetypes import utils

from guegue.alcaldias import alcaldiasMessageFactory as _
from guegue.alcaldias.interfaces import IPlan
from guegue.alcaldias.config import PROJECTNAME

PlanSchema = schemata.ATContentTypeSchema.copy() + atapi.Schema((

    # -*- Your Archetypes field definitions here ... -*-
    atapi.StringField('title',
                searchable=1,
                isMetadata=0,
                accessor='Title',
                widget=atapi.StringWidget(label='Título',
                                      maxlength=150,
                                      size=30,
                                      label_msgid="label_entry_title",
                                      description_msgid="help_entry_title",
                                      i18n_domain="guegue.alcaldias",
                                      description='')),
    atapi.StringField('description',
                searchable=1,
                isMetadata=0,
                accessor='Description',
                widget=atapi.TextAreaWidget(label='Breve descripción',
                                      label_msgid="label_entry_description",
                                      description_msgid="help_entry_description",
                                      i18n_domain="guegue.alcaldias",
                                      description='Entre una breve descripción del plan.')),
   atapi.DateTimeField('fecha',
                searchable=0,
                required=0,
                widget=atapi.CalendarWidget(label='Fecha de Inicio',
                                      label_msgid="label_entry_fecha",
                                      show_hm = False,
                                      description_msgid="help_entry_fecha",
                                      i18n_domain="guegue.alcaldias",
                                      description='Fecha de inicio del proyecto')),
    atapi.DateTimeField('fechafin',
                searchable=0,
                required=0,
                widget=atapi.CalendarWidget(label='Fecha de Finalizacion',
                                      label_msgid="label_entry_fechafin",
                                      show_hm = False,
                                      description_msgid="help_entry_fechafin",
                                      i18n_domain="guegue.alcaldias",
                                      description='Fecha de finalizacion del proyecto')),
    atapi.StringField('dependencia',
                searchable=1,
                isMetadata=0,
                vocabulary="getDependencias",
                widget=atapi.SelectionWidget(label='Dependencia',
                                      label_msgid="label_entry_dependencia",
                                      description_msgid="help_entry_dependencia",
                                      i18n_domain="guegue.alcaldias",
                                      description='')),
    atapi.ImageField('image',
              searchable=0,
              required=0,
              sizes= {'large'   : (768, 768),
                'preview' : (400, 400),
                'mini'    : (200, 200),
                'thumb'   : (128, 128),
                'tile'    :  (64, 64),
                'icon'    :  (32, 32),
                'listing' :  (16, 16),
               },
              widget=atapi.ImageWidget(label='Imagen',
                                label_msgid="label_entry_image",
                                description_msgid="help_entry_image",
                                i18n_domain="guegue.alcaldias",
                                threshold=102400,
                                description='Imagen relacionada al plan.')),
    atapi.FileField('adjunto',
              searchable=0,
              required=0,
              widget=atapi.FileWidget(label='Archivo adjunto',
                                label_msgid="label_entry_adjunto",
                                description_msgid="help_entry_adjunto",
                                i18n_domain="guegue.alcaldias",
                                description='Puede adjuntar un archivo con el plan completo.')),
    atapi.TextField('body',
              searchable=1,
              required=0,
              primary=1,
              default_content_type='text/html',
              default_output_type='text/html',
              allowable_content_types=('text/plain','text/structured', 'text/html', 'text/restructured'),
              widget=atapi.RichWidget(label='Descripcion completa',
                                label_msgid="label_entry_body",
                                description_msgid="help_entry_body",
                                i18n_domain="guegue.alcaldias",
                                description="")),
))

# Set storage on fields copied from ATContentTypeSchema, making sure
# they work well with the python bridge properties.

#PlanSchema['title'].storage = atapi.AnnotationStorage()
#PlanSchema['description'].storage = atapi.AnnotationStorage()

schemata.finalizeATCTSchema(PlanSchema, moveDiscussion=False)

class Plan(base.ATCTContent):
    """Tipo de datos para planes"""
    implements(IPlan)

    def getDependencias(self):
        dl = utils.DisplayList()
        linklist = self.portal_catalog.searchResults(portal_type='Dependencia')
        for link in linklist:
            dl.add(link['id'], link['Title'])
        return dl

    portal_type = "Plan"
    schema = PlanSchema

    security = ClassSecurityInfo()

#    security.declareProtected(View, 'tag')
    def tag(self, **kwargs):
        """Generate image tag using the api of the ImageField
        """
        #if 'title' not in kwargs:
        #    kwargs['title'] = self.getImageCaption()
        return self.getField('image').tag(self, **kwargs)

    #title = atapi.ATFieldProperty('title')
    #description = atapi.ATFieldProperty('description')

atapi.registerType(Plan, PROJECTNAME)
